package dbops;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Description   : Functional Test Script
 * @author angel.tsvetkov
 */
public class Table extends SQLOperator
{		
	public Table(String schemaName, String tableName)
	{
		this.name = tableName;
		this.schema = schemaName;
		columns = new ArrayList<COLUMN>();
	}
	
	/**
	 * Delete all data in specified table
	 */
	public void clear() 
	{	
		try 
		{
			executeStatement("DELETE FROM " + schema + "." + name);
			System.out.println("All rows from " + schema + "." + name + " are completelly deleted!");
			setComment("All rows from "+ schema + "." + name +"are completelly deleted!");
		} catch (SQLException e) {
			setErrorComment("Problem appear during erasing the data from table",e);
		}
	}

	/**
	 * Populate DB table with data from datapool
	 */
	public void insert(String datapoolName)
	{
		try
		{
			INSERT(datapoolName);
		}catch(SQLException e)
		{
			setErrorComment("Problem appear during inserting data!", e);
		}
	}
	
	/**
	 * Populate DB table with data from datapool
	 */
	public void insert(CELL... cells)
	{
		connection.openConnection();
		try {
			insertDataRow(cells);
		} catch (SQLException e) {
			setErrorComment("Problem appear during inserting data from table",e);
		}
		finally
		{
			connection.closeConnection();
		}
	}
	
	/**
	 * Return all DB table columns
	 */
	public List<COLUMN> getAllColumns()
	{
		List<COLUMN> columns = null;
		try {
			columns = getDBColumns();
		} catch (SQLException e) {
			setErrorComment("Problem appear during columns determination.",e);
		}
		return columns;
	}

	/**
	 * Return the count of affected records; parameter - %INDEX$
	 */
	public int execute(String statement, String... args)
	{
		int retValue = 0;
		try
		{
			retValue = executeStatement(formatQuery(statement,args));
		}
		catch(SQLException ex)
		{
			setErrorComment("Statement can't be executed.", ex);
		}
		return retValue;
	}

	/**
	 * Return DataTable representation of DB table
	 */
	public DataTable getTable(int numberOfRecords)
	{
		DataTable table = null;
		try {
			table = SELECT(numberOfRecords);
		} catch (SQLException e) {
			setErrorComment("Problem appear during extracting data!",e);
		}
		return table;
	}
	
	public DataTable getTable()
	{
		return getTable(0);
	}
	
	/**
	 * Return DataTable representation of DB table by specified criteria
	 */
	public DataTable getTable(CELL...cells)
	{
		DataTable table = null;
		try {
			table = SELECT(cells);
		} catch (SQLException e) {
			setErrorComment("Problem appear during extracting data!",e);
		}
		return table;
	}
}


